/**Copyright (c) 2010, Isidoro Trevino de la Garza (chololo@gmail.com)
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
(1)Redistributions of source code must retain the above copyright notice, this list 
of conditions and the following disclaimer. 
(2)Redistributions in binary form must reproduce the above copyright notice, this 
list of conditions and the following disclaimer in the documentation and/or other 
materials provided with the distribution. 
 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
DAMAGE.
*/
package org.citsa.graphx.concept

import com.mxgraph.swing.util.mxGraphTransferable 
import java.awt.Color 
import java.awt.Cursor 
import java.awt.Point 
import java.awt.datatransfer.Transferable 
import java.awt.dnd.DnDConstants 
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;

import javax.swing.ImageIcon 
import javax.swing.JComponent 
import javax.swing.JTable;
import javax.swing.TransferHandler 
import javax.swing.table.AbstractTableModel 
import javax.swing.table.TableColumn 
import javax.swing.table.TableModel;

class MyTable extends JTable implements DropTargetListener {
	
	
	private static final long serialVersionUID = 5841175227984561071L;
	
	Object[][] data;
	
	String[] colNames = [ "A", "B", "C", "D", "E" ] as String[];
	
	@SuppressWarnings("serial")
	public MyTable() {
		data = new Object[30][5];
		for (int i = 0; i < 30; i++) {
			data[i][0] = new Boolean(false);
			data[i][1] = "Column " + i;
			data[i][2] = "Column " + i;
			data[i][3] = "Column " + i;
			data[i][4] = "Column " + i;
		}
		setModel(createModel());
		setTableHeader(null);
		setAutoscrolls(true);
		setGridColor(Color.WHITE);
		TableColumn column = getColumnModel().getColumn(0);
		column.setMaxWidth(20);
		column = getColumnModel().getColumn(2);
		column.setMaxWidth(12);
		column = getColumnModel().getColumn(3);
		column.setMaxWidth(12);
		column = getColumnModel().getColumn(4);
		column.setMaxWidth(12);
		
		setTransferHandler([
					getSourceActions:{
						JComponent c ->
						return COPY_OR_MOVE;
					},
					createTransferable:{
						JComponent c ->
						sourceRow = getSelectedRow();
						//dragSource = JTableRenderer.this;
						//mxRectangle bounds = new mxRectangle(0, 0, MyTable.this
						//		.getWidth(), 20);
						return new mxGraphTransferable(null, null, null);
					}
					
				] as TransferHandler);
		
		setDragEnabled(true);
		setDropTarget(new DropTarget(this, // component
				DnDConstants.ACTION_COPY_OR_MOVE, // actions
				this));
		setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
	}
	
	/**
	 * 
	 */
	public DropTarget getDropTarget() {
		/*	if (!((mxGraphTransferHandler) graphContainer?.getTransferHandler())
		 ?.isLocalDrag()) {
		 return super.getDropTarget();
		 }
		 */
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent)
	 */
	public void dragEnter(DropTargetDragEvent e) {
		
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
	 */
	public void dragOver(DropTargetDragEvent e) {
		/*if (!((mxGraphTransferHandler) graphContainer.getTransferHandler())
		.isLocalDrag()
		&& JTableRenderer.this != dragSource) {
			Point p = e.getLocation();
			int row = rowAtPoint(p);
			getSelectionModel().setSelectionInterval(row, row);
		}*/
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
	 */
	public void dropActionChanged(DropTargetDragEvent dtde) {
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.dnd.DropTargetListener#drop(java.awt.dnd.DropTargetDropEvent)
	 */
	public void drop(DropTargetDropEvent e) {
		if (dragSource != null) {
			e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
			Point p = e.getLocation();
			int targetRow = rowAtPoint(p);
			
			/*Object edge = graph.insertEdge(null, null, null,
					dragSource.cell, JTableRenderer.this.cell, "sourceRow="
					+ sourceRow + ";targetRow=" + targetRow);
			graph.setSelectionCell(edge);
			*/
			// System.out.println("clearing drag source");
			dragSource = null;
			e.dropComplete(true);
		}
		else {
			e.rejectDrop();
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
	 */
	public void dragExit(DropTargetEvent dte) {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 
	 * @return the created table model
	 */
	public TableModel createModel() {
		return [
					
			
			getColumnCount:{
				return colNames.length;
			},
			
			getRowCount:{
				return data.length;
			},
			
			getColumnName:{
				int col ->
				return colNames[col];
			},
			
			getValueAt:{
				int row, int col ->
				return data[row][col];
			},
			
			getColumnClass:{
				c->
				Object value = getValueAt(0, c);
				return (value != null) ? value.getClass() : ImageIcon.class;
			},
			
			isCellEditable:{
				int row, int col ->
				return col == 0;
			},
			
			setValueAt:{
				Object value, int row, int col ->
				data[row][col] = value;
				fireTableCellUpdated(row, col);
			}
		] as AbstractTableModel;
		
	}
	
}

